import type { Executor } from '../';
import type { RoleDto } from '../model/dto';
import type { PageParam, PageResult, RoleSpecification } from '../model/static';

export class RoleController {
    
    constructor(private executor: Executor) {}
    
    async authorize(options: RoleControllerOptions['authorize']): Promise<void> {
        let _uri = '/role/authorize';
        let _separator = _uri.indexOf('?') === -1 ? '?' : '&';
        let _value: any = undefined;
        _value = options.roleId;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'roleId='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        return (await this.executor({uri: _uri, method: 'POST', body: options.body})) as void
    }
    
    async list(options: RoleControllerOptions['list']): Promise<
        ReadonlyArray<RoleDto['DEFAULT']>
    > {
        let _uri = '/role/list';
        let _separator = _uri.indexOf('?') === -1 ? '?' : '&';
        let _value: any = undefined;
        _value = options.roleSpecification.code;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'code='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.roleSpecification.id;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'id='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.roleSpecification.name;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'name='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.roleSpecification.userId;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'userId='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<RoleDto['DEFAULT']>
    }
    
    async page(options: RoleControllerOptions['page']): Promise<
        PageResult<RoleDto['RoleShape/ALL_SCALAR']>
    > {
        let _uri = '/role/page';
        let _separator = _uri.indexOf('?') === -1 ? '?' : '&';
        let _value: any = undefined;
        _value = options.pageParam.currentPage;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'currentPage='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.pageParam.pageSize;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'pageSize='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.specification.code;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'code='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.specification.id;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'id='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.specification.name;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'name='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        _value = options.specification.userId;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'userId='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        return (await this.executor({uri: _uri, method: 'GET'})) as PageResult<RoleDto['RoleShape/ALL_SCALAR']>
    }
}

export type RoleControllerOptions = {
    'authorize': {readonly roleId: number, readonly body: ReadonlyArray<number>},
    'list': {readonly roleSpecification: RoleSpecification},
    'page': {readonly pageParam: PageParam, readonly specification: RoleSpecification}
}